#include "managedatawidget.h"


ManageDataWidget::ManageDataWidget(QWidget *parent) : QWidget(parent)
{
    titleLabel = new QLabel("管理数据");
    titleLabel->setFixedHeight(20);
    titleLabel->setFrameStyle(QLabel::StyledPanel);
    titleLabel->setAlignment(Qt::AlignCenter);

    verifyWidget = new QWidget;
    verifyLabel = new QLabel("请先验证超级管理员身份");
    verifyLabel->setFixedHeight(40);
    verifyLabel->setFrameStyle(QLabel::StyledPanel);
    verifyLabel->setAlignment(Qt::AlignCenter);
    usernameLabel = new QLabel("用户名：");
    usernameLineEdit = new QLineEdit;
    usernameLineEdit->setText("Administrator");
    usernameLineEdit->setEnabled(false);
    passwordLabel = new QLabel("密码：");
    passwordLineEdit = new QLineEdit;
    passwordLineEdit->setEchoMode(QLineEdit::Password);
    verifyOkButton = new QPushButton("验证");
    verifyOkButton->setEnabled(false);
    QGridLayout* verifyLayout = new QGridLayout(verifyWidget);
    verifyLayout->addWidget(verifyLabel, 0, 0, 1, 5);
    verifyLayout->addWidget(usernameLabel, 1, 1, 1, 1);
    verifyLayout->addWidget(usernameLineEdit, 1, 2, 1, 2);
    verifyLayout->addWidget(passwordLabel, 2, 1, 1, 1);
    verifyLayout->addWidget(passwordLineEdit, 2, 2, 1, 2);
    verifyLayout->addWidget(verifyOkButton, 4, 2, 1, 1);

    manageWidget = new QWidget;
    idListWidget = new QListWidget;
    nameListWidget = new QListWidget;
    factoryListWidget = new QListWidget;
    priceListWidget = new QListWidget;
    sumListWidget = new QListWidget;
    sellListWidget = new QListWidget;
    lastListWidget = new QListWidget;

    QIntValidator* validator = new QIntValidator(0, 10000, this);   //设置只能输入0-10000的整数
    idLineEdit = new QLineEdit;
    nameLineEdit = new QLineEdit;
    factoryLineEdit = new QLineEdit;
    priceLineEdit = new QLineEdit;
    sumLineEdit = new QLineEdit;
    sellLineEdit = new QLineEdit;
    lastLineEdit = new QLineEdit;
    idLineEdit->setValidator(validator);
    priceLineEdit->setValidator(validator);
    sellLineEdit->setValidator(validator);
    lastLineEdit->setValidator(validator);

    idLabel = new QLabel("序号");
    idLabel->setFixedHeight(20);
    idLabel->setFrameStyle(QLabel::StyledPanel);
    nameLabel = new QLabel("品牌");
    nameLabel->setFixedHeight(20);
    nameLabel->setFrameStyle(QLabel::StyledPanel);
    factoryLabel = new QLabel("厂家");
    factoryLabel->setFixedHeight(20);
    factoryLabel->setFrameStyle(QLabel::StyledPanel);
    priceLabel = new QLabel("价格/万");
    priceLabel->setFixedHeight(20);
    priceLabel->setFrameStyle(QLabel::StyledPanel);
    sumLabel = new QLabel("总数/辆");
    sumLabel->setFixedHeight(20);
    sumLabel->setFrameStyle(QLabel::StyledPanel);
    sellLabel = new QLabel("已售/辆");
    sellLabel->setFixedHeight(20);
    sellLabel->setFrameStyle(QLabel::StyledPanel);
    lastLabel = new QLabel("余量/辆");
    lastLabel->setFixedHeight(20);
    lastLabel->setFrameStyle(QLabel::StyledPanel);
    addButton = new QPushButton("增加");
    deleteButton = new QPushButton("删除");
    changeButton = new QPushButton("更改");
    updateButton = new QPushButton("刷新");
    quitButton = new QPushButton("返回");
    QHBoxLayout* listWidgetLayout = new QHBoxLayout;
    listWidgetLayout->addWidget(idListWidget);
    listWidgetLayout->addWidget(nameListWidget);
    listWidgetLayout->addWidget(factoryListWidget);
    listWidgetLayout->addWidget(priceListWidget);
    listWidgetLayout->addWidget(sumListWidget);
    listWidgetLayout->addWidget(sellListWidget);
    listWidgetLayout->addWidget(lastListWidget);
    QGridLayout* manageLayout = new QGridLayout(manageWidget);
    manageLayout->addLayout(listWidgetLayout, 0, 0, 3, 7);
    manageLayout->addWidget(idLabel, 3, 0, 1, 1);
    manageLayout->addWidget(nameLabel, 3, 1, 1, 1);
    manageLayout->addWidget(factoryLabel, 3, 2, 1, 1);
    manageLayout->addWidget(priceLabel, 3, 3, 1, 1);
    manageLayout->addWidget(sumLabel, 3, 4, 1, 1);
    manageLayout->addWidget(sellLabel, 3, 5, 1, 1);
    manageLayout->addWidget(lastLabel, 3, 6, 1, 1);
    manageLayout->addWidget(idLineEdit, 4, 0, 1, 1);   //
    manageLayout->addWidget(nameLineEdit, 4, 1, 1, 1);
    manageLayout->addWidget(factoryLineEdit, 4, 2, 1, 1);
    manageLayout->addWidget(priceLineEdit, 4, 3, 1, 1);
    manageLayout->addWidget(sumLineEdit, 4, 4, 1, 1);
    manageLayout->addWidget(sellLineEdit, 4, 5, 1, 1);
    manageLayout->addWidget(lastLineEdit, 4, 6, 1, 1);
    manageLayout->addWidget(addButton, 5, 0, 1, 1);    //
    manageLayout->addWidget(deleteButton, 5, 1, 1, 1);
    manageLayout->addWidget(changeButton, 5, 2, 1, 1);
    manageLayout->addWidget(updateButton, 5, 5, 1, 1);
    manageLayout->addWidget(quitButton, 5, 6, 1, 1);
    manageWidget->hide();

    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->addSpacing(20);
    mainLayout->addWidget(titleLabel);
    mainLayout->addWidget(verifyWidget);
    mainLayout->addWidget(manageWidget);

    connect(usernameLineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotVerifyLineEditTextChange()));
    connect(passwordLineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotVerifyLineEditTextChange()));
    connect(verifyOkButton, SIGNAL(clicked(bool)), this, SLOT(slotVerifyOkButton()));
    connect(addButton, SIGNAL(clicked(bool)), this, SLOT(slotAddButton()));
    connect(deleteButton, SIGNAL(clicked(bool)), this, SLOT(slotDeleteButton()));
    connect(changeButton, SIGNAL(clicked(bool)), this, SLOT(slotChangeButton()));
    connect(updateButton, SIGNAL(clicked(bool)), this, SLOT(showListWidget()));
    connect(quitButton, SIGNAL(clicked(bool)), this, SLOT(slotQuitButton()));

    connect(idLineEdit, SIGNAL(textChanged(QString)), SLOT(slotIdlineEditTextChange(QString)));
    connect(sellLineEdit, SIGNAL(textChanged(QString)), SLOT(slotNumberLienEditTextChange()));
    connect(lastLineEdit, SIGNAL(textChanged(QString)), SLOT(slotNumberLienEditTextChange()));
}

void ManageDataWidget::showListWidget()
{
    idListWidget->clear();
    nameListWidget->clear();
    factoryListWidget->clear();
    priceListWidget->clear();
    sumListWidget->clear();
    sellListWidget->clear();
    lastListWidget->clear();

    idListWidget->addItem("序号");
    nameListWidget->addItem("品牌");
    factoryListWidget->addItem("厂家");
    priceListWidget->addItem("价格/万");
    sumListWidget->addItem("总数/辆");
    sellListWidget->addItem("已售/辆");
    lastListWidget->addItem("余量/辆");

    QSqlQuery query;
    query.exec("select id,name,factory,price,sum,sell,last from brand order by id");
    while(query.next())
    {
        idListWidget->addItem(query.value(0).toString());
        nameListWidget->addItem(query.value(1).toString());
        factoryListWidget->addItem(query.value(2).toString());
        priceListWidget->addItem(query.value(3).toString());
        sumListWidget->addItem(query.value(4).toString());
        sellListWidget->addItem(query.value(5).toString());
        lastListWidget->addItem(query.value(6).toString());
    }
    initialize();
}
void ManageDataWidget::slotVerifyOkButton()
{
    QSqlQuery query;
    query.exec(QString("select password from passwordtable where username='Administrator'"));
    query.next();
    if(query.value(0).toString() == passwordLineEdit->text())
    {
        QMessageBox::information(this, "提示", "验证成功！", QMessageBox::Ok);
        passwordLineEdit->clear();
        showListWidget();
        verifyWidget->hide();
        manageWidget->show();
    }
    else
    {
        QMessageBox::warning(this, "警告", "帐号与密码不符，验证失败！", QMessageBox::Ok);
    }
}
void ManageDataWidget::slotVerifyLineEditTextChange()
{
    if(!usernameLineEdit->text().isEmpty() && !passwordLineEdit->text().isEmpty())
    {
        verifyOkButton->setEnabled(true);
    }
    else
    {
        verifyOkButton->setEnabled(false);
    }
}
void ManageDataWidget::initialize()
{
    idLineEdit->clear();
    nameLineEdit->clear();
    factoryLineEdit->clear();
    priceLineEdit->clear();
    sumLineEdit->clear();
    sellLineEdit->clear();
    lastLineEdit->clear();

    idLineEdit->setEnabled(true);
    nameLineEdit->setEnabled(false);
    factoryLineEdit->setEnabled(false);
    priceLineEdit->setEnabled(false);
    sumLineEdit->setEnabled(false);
    sellLineEdit->setEnabled(false);
    lastLineEdit->setEnabled(false);

    addButton->setEnabled(false);
    deleteButton->setEnabled(false);
    changeButton->setEnabled(false);
}
void ManageDataWidget::slotAddButton()
{
    if(!nameLineEdit->text().isEmpty()&&
       !factoryLineEdit->text().isEmpty()&&
       !priceLineEdit->text().isEmpty()&&
       !sellLineEdit->text().isEmpty()&&
       !lastLineEdit->text().isEmpty())
    {
        QString command;
        command = QString("insert into brand values('%1', '%2', '%3', %4, %5, %6, %7)")
                .arg(idLineEdit->text()).arg(nameLineEdit->text()).arg(factoryLineEdit->text())
                .arg(priceLineEdit->text()).arg(sumLineEdit->text()).arg(sellLineEdit->text())
                .arg(lastLineEdit->text());
        QSqlQuery query;
        bool result = query.exec(command);
        if(result)
        {
            QMessageBox::information(this, "提示", "添加成功！", QMessageBox::Ok);
            showListWidget();
        }
        else
        {
            QMessageBox::information(this, "提示", "添加失败！", QMessageBox::Ok);
        }
    }
    else
    {
        QMessageBox::warning(this, "提示", "请完善信息！", QMessageBox::Ok);
    }
}
void ManageDataWidget::slotDeleteButton()
{
    QSqlQuery query;
    bool result = query.exec(QString("delete from brand where id='%1'").arg(idLineEdit->text()));
    if(result)
    {
        QMessageBox::information(this, "提示", "删除成功！", QMessageBox::Ok);
        showListWidget();
    }
    else
    {
        QMessageBox::information(this, "提示", "删除失败！", QMessageBox::Ok);
    }
}
void ManageDataWidget::slotChangeButton()
{
    if(!nameLineEdit->text().isEmpty()&&
       !factoryLineEdit->text().isEmpty()&&
       !priceLineEdit->text().isEmpty()&&
       !sellLineEdit->text().isEmpty()&&
       !lastLineEdit->text().isEmpty())
    {
        QSqlQuery query;
        query.exec(QString("delete from brand where id='%1'").arg(idLineEdit->text()));
        QString command;
        command = QString("insert into brand values('%1', '%2', '%3', %4, %5, %6, %7)")
                .arg(idLineEdit->text()).arg(nameLineEdit->text()).arg(factoryLineEdit->text())
                .arg(priceLineEdit->text()).arg(sumLineEdit->text()).arg(sellLineEdit->text())
                .arg(lastLineEdit->text());
        bool result = query.exec(command);
        if(result)
        {
            QMessageBox::information(this, "提示", "更新数据成功！", QMessageBox::Ok);
            showListWidget();
        }
        else
        {
            QMessageBox::information(this, "提示", "更新数据失败！", QMessageBox::Ok);
        }
    }
    else
    {
        QMessageBox::warning(this, "提示", "请完善信息！", QMessageBox::Ok);
    }
}
void ManageDataWidget::slotQuitButton()
{
    idListWidget->clear();
    nameListWidget->clear();
    factoryListWidget->clear();
    priceListWidget->clear();
    sumListWidget->clear();
    sellListWidget->clear();
    lastListWidget->clear();

    initialize();

    manageWidget->hide();
    verifyWidget->show();
}
void ManageDataWidget::slotIdlineEditTextChange(QString id)
{
    nameLineEdit->clear();
    factoryLineEdit->clear();
    priceLineEdit->clear();
    sumLineEdit->clear();
    sellLineEdit->clear();
    lastLineEdit->clear();

    QSqlQuery query;
    query.exec(QString("select name,factory,price,sum,sell,last from brand where id='%1'").arg(id));
    if(query.next())    //数据库内有信息
    {
        nameLineEdit->setEnabled(false);
        factoryLineEdit->setEnabled(false);
        priceLineEdit->setEnabled(true);
        sellLineEdit->setEnabled(true);
        lastLineEdit->setEnabled(true);

        nameLineEdit->setText(query.value(0).toString());
        factoryLineEdit->setText(query.value(1).toString());
        priceLineEdit->setText(query.value(2).toString());
        sumLineEdit->setText(query.value(3).toString());
        sellLineEdit->setText(query.value(4).toString());
        lastLineEdit->setText(query.value(5).toString());

        addButton->setEnabled(false);
        deleteButton->setEnabled(true);
        changeButton->setEnabled(true);
    }
    else if(!idLineEdit->text().isEmpty())     //录入新车
    {
        nameLineEdit->setEnabled(true);
        factoryLineEdit->setEnabled(true);
        priceLineEdit->setEnabled(true);
        sellLineEdit->setEnabled(true);
        lastLineEdit->setEnabled(true);

        addButton->setEnabled(true);
        deleteButton->setEnabled(false);
        changeButton->setEnabled(false);
    }
    else
    {
        initialize();
    }

}
void ManageDataWidget::slotNumberLienEditTextChange()
{
    int num = sellLineEdit->text().toInt() + lastLineEdit->text().toInt();
    sumLineEdit->setText(QString("%1").arg(num));
}
